<?php

class Login extends Controller {

	function Login()
	{
		parent::Controller();
		//		$user = array();
		//		$user['username'] = 'admin';
		//        $user['password'] = 'admin';
		//        $user['profile'] = 'Administrator';
		//        $user['create_by'] = 'Pun';
		//        $this->load->model('usermodel');

//var_dump($this->session->userdata('profile'));		//		$this->usermodel->addUser($user);
	}

	function index()
	{
	  
		// LOAD HELPERS
		$this->load->helper(array('form', 'url', 'security'));

		$this->load->model('usermodel');
		$this->form_validation->set_rules('userName', 'username', 'required');
		$this->form_validation->set_rules('password', 'password', 'required');
		$this->form_validation->set_error_delimiters('<span style="font-size:10pt; color:#FF0000;font-style:italic">','</span>');
		$this->form_validation->set_message('required', ' ');
		// SET VALIDATION RULES
		$msg = null;
		// has the form been submitted and with valid form info (not empty values)
		//		if($this->input->post('login'))
		//		{
		if($this->form_validation->run())
		{
			$userName = $this->input->post('userName');
			$password = $this->input->post('password');

			$data = $this->usermodel->getUserByUsername($userName);
			if($data != null && $data['username'] == $userName)
			{
				if(dohash($password) == $data['password'])
				{
					if($data['profile'] == 'employee') {
						$this->load->model('empModel');
						$emp = $this->empModel->getPermissionByUsername($userName);
						if($emp != null && $emp['is_active'] == 1) {
							$newdata = array(
							                  'username'  => $userName,
							                  'profile'   => $data['profile'],
							                  'logged_in' => TRUE,
											  'menu_a' => $emp['a'],
											  'menu_b' => $emp['b'],
												'menu_c' => $emp['c'],
												'menu_d' => $emp['d'],
												'menu_e' => $emp['e']
							
							);
						} else {
							$newdata = array(
					                  'username'  => $userName,
					                  'profile'   => $data['profile'],
					                  'logged_in' => false
							);
							$this->session->set_userdata($newdata);
							$this->session->set_flashdata('message', 'ตอนนี้คุณกำลังถูกระงับสิทธิ์การใช้เว็บไซต์ กรุณาติดต่อธุรการค่ะ');
							redirect('login/index/');
						}
					} else {
						$newdata = array(
						                  'username'  => $userName,
						                  'profile'   => $data['profile'],
						 'menu_a' => 'N',
											  'menu_b' => 'N',
												'menu_c' => 'N',
												'menu_d' => 'N',
												'menu_e' => 'N',
						                  'logged_in' => TRUE
						
						);
					}
					$this->session->set_userdata($newdata);

					$this->load->model('logintransactionmodel');
					$this->logintransactionmodel->add($data['profile']);
	
					if($data['profile'] == 'Tutor') {
						// check expire date
						$this->load->model('TutorModel');
						$tt = $this->TutorModel->getByUserName($userName);
						if($tt != null && isset($tt['user_expire'])) {
							$exp_date = $tt['user_expire'];
							$todays_date = date("Y-m-d");
							$today = strtotime($todays_date);
							$expiration_date = strtotime($exp_date);
							if ($expiration_date < $today) {
								$newdata = array(
						                  'username'  => $userName,
						                  'profile'   => $data['profile'],
						                  'logged_in' => false
								);
								$this->session->set_userdata($newdata);
								$this->session->set_flashdata('message', 'ตอนนี้คุณกำลังถูกระงับสิทธิ์การใช้เว็บไซต์ กรุณาติดต่อธุรการค่ะ');
								redirect('login/index/');
							}
						} else {
							$newdata = array(
						                  'username'  => $userName,
						                  'profile'   => $data['profile'],
						                  'logged_in' => false
								);
							$this->session->set_userdata($newdata);
							$this->session->set_flashdata('message', 'ตอนนี้คุณกำลังถูกระงับสิทธิ์การใช้เว็บไซต์ กรุณาติดต่อธุรการค่ะ');
							redirect('login/index/');
						}
					}
				}
				else
				{
					$this->session->set_flashdata('message', 'username หรือ รหัสผ่านไม่ถูกต้อง');
					redirect('login/index/');
				}
			}
			else
			{
				$this->session->set_flashdata('message', 'username หรือ รหัสผ่านไม่ถูกต้อง');
				redirect('login/index/');
			}
		}
		//		}

		if ($this->session->userdata('logged_in') == TRUE)
		{
			if($this->session->userdata('profile') == 'Tutor') {
				redirect('TutorLogin');
			} else {
				$this->load->view('header');
				$this->load->view('welcome');
				$this->load->view('footer');
			}
		} else {
			$this->load->model('filemanagementmodel');
			$msg['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
			$msg['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
			$this->load->view('f_header', $msg);
			$this->load->view('f_login');
			$this->load->view('f_footer');
		}
	}

	function logsearch()
	{
		if ($this->session->userdata('logged_in') != TRUE)
		{
			redirect('login/index');
		}
	  
		$keyword = $this->input->post('txtName') ? $this->input->post('txtName') : null;
		$profile = $this->input->post('profile')? $this->input->post('profile') : null;;
		$times = $this->input->post('logTime')? $this->input->post('logTime') : null;;
		if($profile == '---ทั้งหมด---')
		$profile = null;
		$data = array('keyword' => $keyword, 'profile' => $profile, 'times' => $times);
		$this->load->model('logintransactionmodel');
		$result['logs'] = $this->logintransactionmodel->getByCriteria($data);
		$result['keyword'] = $keyword;
		$this->load->view('header');
		$this->load->view('log-search-form', $result);
		$this->load->view('footer');
	}

	function logout()
	{
		$this->session->sess_destroy();
		redirect('login/index/');
	}
}

